package com.zgh.rbac.module;

import com.xxg.base.module.BaseEntity;

import java.io.Serializable;
import java.util.*;

public class Menu extends BaseEntity implements Serializable,Comparable<Menu>{

	private static final long serialVersionUID = 1L;

	private String menuName;	//菜单名称
	private String url;		//菜单地址
	private String smallIcon;		//小图标
	private String bigIcon;		//大图标
	private long parentId;		//上级ID
	private String menuType;		//菜单类型    业务类或者办公类
	private String module;		//所属功能模块
	private String status;		//状态，是否启用
	private String display;		//是否显示
	private int menuIndex;		//序号
	private String target;		//打开方式
	private String deleteStatus;     //删除状态
	private String sort;             //菜单分类类型：1pc端菜单，2app端菜单

	private String systemName;		//所属系统，内网或外网
	private String addType;		//添加方式
	private String menuLevel;	//菜单层级
	private String apply;     //申请类菜单

	private String ifMenu;

	//以下信息不存数据库
	private List<Menu> children=new ArrayList<Menu>();
	private String parentMenuName;	//上级菜单的名称

	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getSmallIcon() {
		return smallIcon;
	}
	public void setSmallIcon(String smallIcon) {
		this.smallIcon = smallIcon;
	}
	public String getBigIcon() {
		return bigIcon;
	}
	public void setBigIcon(String bigIcon) {
		this.bigIcon = bigIcon;
	}
	public long getParentId() {
		return parentId;
	}
	public void setParentId(long parentId) {
		this.parentId = parentId;
	}
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		this.module = module;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getDisplay() {
		return display;
	}
	public void setDisplay(String display) {
		this.display = display;
	}
	public int getMenuIndex() {
		return menuIndex;
	}
	public void setMenuIndex(int menuIndex) {
		this.menuIndex = menuIndex;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getSystemName() {
		return systemName;
	}
	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}
	public String getMenuType() {
		return menuType;
	}
	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}


	public String getApply() {
		return apply;
	}

	public void setApply(String apply) {
		this.apply = apply;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getParentMenuName() {
		return parentMenuName;
	}
	public void setParentMenuName(String parentMenuName) {
		this.parentMenuName = parentMenuName;
	}

	public String getDeleteStatus() {
		return deleteStatus;
	}

	public void setDeleteStatus(String deleteStatus) {
		this.deleteStatus = deleteStatus;
	}

	public String getAddType() {
		return addType;
	}
	public void setAddType(String addType) {
		this.addType = addType;
	}
	public String getMenuLevel() {
		return menuLevel;
	}
	public void setMenuLevel(String menuLevel) {
		this.menuLevel = menuLevel;
	}
	public String getIfMenu() {
		return ifMenu;
	}
	public void setIfMenu(String ifMenu) {
		this.ifMenu = ifMenu;
	}
	public List<Menu> getChildren() {
		return children;
	}
	public void setChildren(List<Menu> children) {
		this.children = children;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj==null){
			return false;
		}
		if(obj instanceof Menu){
			return  this.getId().equals(((Menu) obj).getId());
		}else{
			return false;
		}

	}

	@Override
	public int compareTo(Menu menu) {
		return this.getMenuIndex()-menu.getMenuIndex();
	}


	public static List<Menu> toTree(List<Menu> list) {
		Menu root=generateTreeRoot(list);
		return root.getChildren();
	}

	private static Menu generateTreeRoot(List<Menu> nodeList) {
		Menu root = new Menu();
		if(nodeList==null || nodeList.isEmpty()){
			return root;
		}
		Stack<Menu> stack = new Stack<Menu>();

		while (nodeList.size() > 0) {
			Menu cNode = (Menu) nodeList.get(0);
			nodeList.remove(cNode);
			stack.push(cNode);

			while (cNode != null) {
				cNode = (Menu) stack.pop();
				Menu chNode;
				if ((chNode = getChildren(cNode, nodeList)) != null) {
					stack.push(cNode);
					nodeList.remove(chNode);
					stack.push(chNode);
				} else {
					Menu pNode;
					if (stack.size() > 0) {
						pNode = (Menu) stack.pop();
						pNode.getChildren().add(cNode);
						Collections.sort(pNode.getChildren());
						stack.push(pNode);
					} else if ((pNode = getParent(cNode, nodeList)) != null) {
						nodeList.remove(pNode);
						stack.push(pNode);
						pNode.getChildren().add(cNode);
						Collections.sort(pNode.getChildren());
					} else {
						root.getChildren().add(cNode);
						cNode = null;
					}
				}
			}
		}
		Collections.sort(root.getChildren());
		return root;
	}

	private static Menu getChildren(Menu node, List<Menu> list) {
		Iterator<Menu> var3 = list.iterator();

		Menu t;
		do {
			if (!var3.hasNext()) {
				return null;
			}

			t = (Menu) var3.next();
		} while (t.getParentId()==0 || t.getParentId()!=node.getId().longValue());

		return t;
	}

	private static Menu getParent(Menu node, List<Menu> list) {
		Iterator<Menu> var3 = list.iterator();

		Menu t;
		do {
			if (!var3.hasNext()) {
				return null;
			}

			t = (Menu) var3.next();
		} while (t.getId().longValue()!=node.getParentId());

		return t;
	}

	@Override
	public String toString() {
		return "Menu{" +
				"id=" + super.getId() +
//				", dataCode='" + super.getDataCode() + '\'' +
//				", note='" + super.getNote() + '\'' +
//				", creater=" + super.getCreater() +
//				", createName='" + super.getCreateName() + '\'' +
//				", createTime='" + super.getCreateTime() + '\'' +
//				", updater=" + super.getUpdater() +
//				", updateName='" + super.getUpdateName() + '\'' +
//				", updateTime='" + super.getUpdateTime() + '\'' +
				", menuName='" + menuName + '\'' +
				", url='" + url + '\'' +
//				", smallIcon='" + smallIcon + '\'' +
//				", bigIcon='" + bigIcon + '\'' +
//				", parentId=" + parentId +
//				", menuType='" + menuType + '\'' +
//				", module='" + module + '\'' +
//				", status='" + status + '\'' +
//				", display='" + display + '\'' +
//				", menuIndex=" + menuIndex +
//				", target='" + target + '\'' +
//				", deleteStatus='" + deleteStatus + '\'' +
//				", sort='" + sort + '\'' +
//				", systemName='" + systemName + '\'' +
//				", addType='" + addType + '\'' +
//				", menuLevel='" + menuLevel + '\'' +
//				", apply='" + apply + '\'' +
				", ifMenu='" + ifMenu + '\'' +
				", children=" + children +
//				", parentMenuName='" + parentMenuName + '\'' +
				'}';
	}
}
